package org.jboss.seam.wiki.core.preferences.editor;

import java.io.Serializable;

import org.hibernate.validator.constraints.Range;
import org.jboss.seam.wiki.preferences.metamodel.PreferenceEntity;
import org.springframework.stereotype.Component;

@Component("editorNumberRange")
public class EditorNumberRange implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 2920141075905752749L;

	public long getRangeMin(PreferenceEntity.Property property) {
        checkAnnotation(property);
        return property.getField().getAnnotation(Range.class).min();
    }

    public long getRangeMax(PreferenceEntity.Property property) {
        checkAnnotation(property);
        return property.getField().getAnnotation(Range.class).max();
    }

    private void checkAnnotation(PreferenceEntity.Property property) {
        if (!property.getField().isAnnotationPresent(Range.class)) {
            throw new IllegalArgumentException("property does not have @Range annotation " + property);
        }
    }

}
